package com.heima.feign.clients.fallback;

import com.heima.feign.clients.ArticleClient;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ArticleClientFallbackFactory implements FallbackFactory<ArticleClient> {
    @Override
    public ArticleClient create(Throwable throwable) {

        log.info("即将调用 WemediaClient 远程调用的服务降级策略...");

        return new ArticleClient() {
            @Override
            public ResponseResult<ApAuthor> findByUserId(Integer userId) {
                log.info("请求参数: {}", userId);
                log.info("执行远程调用时, AuthroClientFallbackFactory findByUserId 出现异常: {}", throwable.getMessage());
                return ResponseResult.errorResult(AppHttpCodeEnum.REMOTE_SERVER_ERROR);
            }

            @Override
            public ResponseResult save(ApAuthor apAuthor) {
                log.info("请求参数: {}", apAuthor);
                log.info("执行远程调用时, AuthorClientFallbackFactory save 出现异常: {}", throwable.getMessage());
                return ResponseResult.errorResult(AppHttpCodeEnum.REMOTE_SERVER_ERROR);
            }
        };
    }
}
